//
//  PPMineViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/30.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PPMineInfoModel.h"
#import "PPMineMoneyInfoModel.h"

#import "PPMineNormalCell.h"
#import "PPMineMoneyCell.h"
#import "PPMineInfoCell.h"
#import "PPMineOrderCell.h"
#import "PPMineEditionCell.h"

#import "PPMineViewController.h"

//用户信息
#import "PersionInfoViewController.h"

//订单中心
#import "OrderCenterViewController.h"

//我的资产
#import "MyAssetsViewController.h"

//累计收益
#import "IncomeListViewController.h"

@interface PPMineViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;


@property (nonatomic, strong) PPMineInfoModel *infoModel;
@property (nonatomic, strong) PPMineMoneyInfoModel *moneyInfoModel;

@property (nonatomic, strong) NSMutableArray <PPMineNormalModel *>*mineConfigModelArray;

@end

@implementation PPMineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
        [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
            
            [self request_MineData];
        }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self prepareConfigData];
}

- (void)request_MineData {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MINE_INFO];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    self.infoModel.userIconUrl = [resultDic valueForKey:@"avatar"];
                    
                    [[TTUserModel sharedObject].userModelDefault setValue:[resultDic valueForKey:@"avatar"] forKey:@"user_icon"];
                    [[TTUserModel sharedObject].userModelDefault setValue:[resultDic valueForKey:@"username"] forKey:@"uer_name"];

                    [[TTUserModel sharedObject].userModelDefault synchronize];
                    
                    self.infoModel.userName = [resultDic valueForKey:@"username"];
                    
                    BOOL isAuth = [[resultDic valueForKey:@"certification"] boolValue];
                    if (isAuth) {
                        self.infoModel.isRealNameAuth = YES;
                        [[TTUserModel sharedObject].userModelDefault setValue:@(Comment_Verify_Pass) forKey:@"user_vertify"];
                    } else {
                        self.infoModel.isRealNameAuth = NO;
                        [[TTUserModel sharedObject].userModelDefault setValue:@(Comment_Verify_Return) forKey:@"user_vertify"];
                    }
                    
                    //我的资产
                    self.moneyInfoModel.myAssets = [[resultDic valueForKey:@"remainder"] doubleValue];
                    //累计收益
                    self.moneyInfoModel.accumulatedIncome = [[resultDic valueForKey:@"bounty"] doubleValue];
                }
                
                [self.tableview reloadData];
                
            } else {
                
                //检查到未登录
                if ([code isEqualToString:@"401"]) {
                    
                } else {
                    
                    [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                    [SVProgressHUD dismissWithDelay:1];
                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:kTTViewStateNoData btnTitle:@"重新加载" didPressButton:^{
                        [self request_MineData];
                    }];
                }
            }
            
        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismissWithDelay:0.5];

        WEAKSELF
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
            [weakSelf request_MineData];
        }];
    }];
    
}

- (void)prepareConfigData {
    
   NSArray *config = @[
                       @{@"icon":@"mine_yaoqing",@"title":@"推荐有礼",@"type":@"RecommandGiftViewController",@"ID":@""},
                       @{@"icon":@"mine_coupon",@"title":@"一拍必得券",@"type":@"PPCouponViewController",@"ID":@""},
                       @{@"icon":@"mine_address",@"title":@"收货地址",@"type":@"AddressManagerViewController",@"ID":@""},
                       @{@"icon":@"mine_safe",@"title":@"安全中心",@"type":@"SafeCenterViewController",@"ID":@""},
                       @{@"icon":@"mine_help",@"title":@"帮助中心",@"type":@"HelpCenterViewController",@"ID":@""},
                       @{@"icon":@"mine_guanyuwomen",@"title":@"关于我们",@"type":@"",@"ID":@""},
                     ];
    
    
    self.mineConfigModelArray = @[].mutableCopy;
    
    for (int i = 0; i<config.count; i++) {
        
        PPMineNormalModel *model = [[PPMineNormalModel alloc] init];
        model.imageName = [config[i] valueForKey:@"icon"];;
        model.title = [config[i] valueForKey:@"title"];;
        model.type = [config[i] valueForKey:@"type"];

        [self.mineConfigModelArray addObject:model];
    }
    
}

- (void)initView {
    self.title = @"我的";
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"PPMineNormalCell" bundle:nil] forCellReuseIdentifier:@"PPMineNormalCellID"];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"PPMineMoneyCell" bundle:nil] forCellReuseIdentifier:@"PPMineMoneyCellID"];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"PPMineOrderCell" bundle:nil] forCellReuseIdentifier:@"PPMineOrderCellID"];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"PPMineInfoCell" bundle:nil] forCellReuseIdentifier:@"PPMineInfoCellID"];
    
    [self.tableview registerNib:[UINib nibWithNibName:@"PPMineEditionCell" bundle:nil] forCellReuseIdentifier:@"PPMineEditionCellID"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    WEAKSELF
    if (indexPath.row == 0) {
        PPMineInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPMineInfoCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        [cell fillCellWithModel:self.infoModel];
        
        cell.mineInfocellClickBlock = ^(MineInfoCellActionType type) {
          
            switch (type) {
                case Mine_Go_UserInfo:{
                    
                    PersionInfoViewController *VC =  [PersionInfoViewController new];
                    [self.navigationController pushViewController:VC animated:YES];
                                        
                }break;
                    
                default:
                    break;
            }
            
        };
        
        return cell;
        
        
    } else if (indexPath.row == 1) {
        PPMineMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPMineMoneyCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        [cell fillCellWithModel:self.moneyInfoModel];
        
        cell.mineInfocellClickBlock = ^(MineMoneyCellActionType type) {
          
            switch (type) {
                    
                case Mine_Go_TotalIncome:
                {
                    IncomeListViewController *VC =  [IncomeListViewController new];
                    [self.navigationController pushViewController:VC animated:YES];
                }break;
                    
                case Mine_Go_MyAution:
                {
                    MyAssetsViewController *assetsVC =  [MyAssetsViewController new];
                    assetsVC.infoModel = self.infoModel;
                    [self.navigationController pushViewController:assetsVC animated:YES];
                } break;
                    
                default:
                    break;
            }
        };
        
        return cell;
        
    } else if (indexPath.row == 2) {
        PPMineOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPMineOrderCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        cell.handleOrderClickWithType = ^(ETTOrderListType type) {
            
            OrderCenterViewController *VC = [[OrderCenterViewController alloc] init];
            VC.selectType = type;
            [weakSelf.navigationController pushViewController:VC animated:YES];
        };
        
        return cell;
        
    } else if (indexPath.row > 2 && indexPath.row < self.mineConfigModelArray.count + 3) {
    
        PPMineNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPMineNormalCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        
        [cell fillCellWithModel:self.mineConfigModelArray[indexPath.row  - 3]];
    
        return cell;
        
    } else {
        
        PPMineEditionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PPMineEditionCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.mineConfigModelArray.count + 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        return 100;
        
    } else if (indexPath.row == 1){
        return 90;
        
    } else if (indexPath.row == 2){
        return 150;
        
    } else if (indexPath.row > 2 && indexPath.row < self.mineConfigModelArray.count + 3) {
        
        return 50;
        
    }   else {
        
        return 130;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        PersionInfoViewController *VC =  [PersionInfoViewController new];
        [self.navigationController pushViewController:VC animated:YES];
        
    } else if (indexPath.row == 1){
        
    } else if (indexPath.row == 2) {
        
        [[TTJumpManager share] pushViewControllerWithClassName:@"OrderCenterViewController"];

    } else if (indexPath.row > 2 && indexPath.row < self.mineConfigModelArray.count + 2) {
        
        [[TTJumpManager share] pushViewControllerWithClassName:self.mineConfigModelArray[indexPath.row - 3].type];

    }   else if (indexPath.row == self.mineConfigModelArray.count + 2){
        
        PP_jumpModel *model = [PP_jumpModel new];
        model.type = CommentClickType_HtmlPage;
        model.ID = [NSString stringWithFormat:@"%@%@",TT_Global_H5_Domain,TT_HTML_ABOUT_US];
        [[TTJumpManager share] pushViewControllerWithModel:model];
        
    } else {
        
    }
}

- (PPMineInfoModel *)infoModel{
    if (!_infoModel) {
        _infoModel = [[PPMineInfoModel alloc] init];
    }
    return _infoModel;
}

- (PPMineMoneyInfoModel *)moneyInfoModel {
    
    if (!_moneyInfoModel) {
        _moneyInfoModel = [[PPMineMoneyInfoModel alloc] init];
    }
    return _moneyInfoModel;
}

@end
